home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / apply.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  249 lines

  1.  
  2. #include <windows.h>  
  3. #include "apply.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "PropSheet_Apply"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK PageProc( HWND hDlg, UINT message,        
  108.                            WPARAM wParam, LPARAM lParam )
  109. {
  110. static NMHDR* lphdr;
  111.  
  112.    switch (message) 
  113.    {
  114.        case WM_INITDIALOG: 
  115.              EnableWindow( GetDlgItem( hDlg, IDC_SAVE ), FALSE );
  116.              return (TRUE);
  117.  
  118.        case WM_COMMAND :
  119.              switch( LOWORD( wParam ) )
  120.              {
  121.                 case IDC_EDIT1 :
  122.                 case IDC_EDIT2 :
  123.                 case IDC_EDIT3 :
  124.                         if ( HIWORD( wParam ) == EN_CHANGE )
  125.                         {
  126.                            PropSheet_Changed( GetParent( hDlg ), hDlg );
  127.                            EnableWindow( GetDlgItem( hDlg, IDC_SAVE ), TRUE );
  128.                         }
  129.                         break;
  130.  
  131.                 case IDC_SAVE :
  132.                         // Permanently save the changes.
  133.  
  134.                         // Apply changes to the object.
  135.                         //.............................
  136.                         PropSheet_Apply( GetParent( hDlg ) );
  137.  
  138.                         // Change the OK button to Close.
  139.                         //...............................
  140.                         PropSheet_CancelToClose( GetParent( hDlg ) );
  141.                         break;
  142.              }
  143.              break;
  144.  
  145.        case WM_NOTIFY :
  146.              lphdr = (NMHDR*)lParam;
  147.              if ( lphdr->code == PSN_APPLY )
  148.              {
  149.                 // Apply changes.
  150.  
  151.                 // Gray out the Apply and Save buttons.
  152.                 //.....................................
  153.                 PropSheet_UnChanged( GetParent( hDlg ), hDlg );
  154.                 EnableWindow( GetDlgItem( hDlg, IDC_SAVE ), FALSE );
  155.              }
  156.              break;
  157.    }
  158.  
  159.    return( FALSE );
  160. }
  161.  
  162.  
  163. VOID DisplayProperties( HWND hWnd )
  164. {
  165.    PROPSHEETPAGE   psp;
  166.    PROPSHEETHEADER psh;
  167.    HPROPSHEETPAGE  hpsp;
  168.  
  169.    psp.dwSize      = sizeof( PROPSHEETPAGE );
  170.    psp.dwFlags     = PSP_DEFAULT;
  171.    psp.hInstance   = hInst;
  172.    psp.pszTemplate = "EDITPAGE";
  173.    psp.pfnDlgProc  = (DLGPROC)PageProc;
  174.    hpsp = CreatePropertySheetPage( &psp );
  175.  
  176.    memset( &psh, 0, sizeof( PROPSHEETHEADER ) );
  177.    psh.dwSize     = sizeof( PROPSHEETHEADER );
  178.    psh.dwFlags    = PSH_USEICONID;
  179.    psh.hInstance  = hInst;
  180.    psh.hwndParent = hWnd;
  181.    psh.pszIcon    = "SMALL";
  182.    psh.nPages     = 1;
  183.    psh.phpage     = &hpsp;
  184.    psh.pszCaption = "Properties";
  185.  
  186.    PropertySheet( &psh );
  187. }
  188.  
  189.  
  190. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  191. {
  192.    switch( uMsg )
  193.    {
  194.       case WM_CREATE :
  195.               InitCommonControls();
  196.               break;
  197.  
  198.       case WM_COMMAND :
  199.               switch( LOWORD( wParam ) )
  200.               {
  201.                  case IDM_TEST :
  202.                         DisplayProperties( hWnd );
  203.                         break;
  204.  
  205.                  case IDM_ABOUT :
  206.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  207.                         break;
  208.  
  209.                  case IDM_EXIT :
  210.                         DestroyWindow( hWnd );
  211.                         break;
  212.               }
  213.               break;
  214.       
  215.       case WM_DESTROY :
  216.               PostQuitMessage(0);
  217.               break;
  218.  
  219.       default :
  220.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  221.    }
  222.  
  223.    return( 0L );
  224. }
  225.  
  226.  
  227. LRESULT CALLBACK About( HWND hDlg,           
  228.                         UINT message,        
  229.                         WPARAM wParam,       
  230.                         LPARAM lParam)
  231. {
  232.    switch (message) 
  233.    {
  234.        case WM_INITDIALOG: 
  235.                return (TRUE);
  236.  
  237.        case WM_COMMAND:                              
  238.                if (   LOWORD(wParam) == IDOK         
  239.                    || LOWORD(wParam) == IDCANCEL)    
  240.                {
  241.                        EndDialog(hDlg, TRUE);        
  242.                        return (TRUE);
  243.                }
  244.                break;
  245.    }
  246.  
  247.    return (FALSE); 
  248. }
  249.